var e = getApp(), a = require("../../utils/config");

Page({
    data: {
        videoad: "adunit-2c444468a3401bb5"
    },
    onLoad: function() {
        e.setTitle(), e.setNavigationBar(), this.getIndexData();
        var a = this;
        "" != wx.getStorageSync("user_id") ? a.setData({
            is_login: 1
        }) : a.setData({
            is_login: 0
        });
    },
    onShow: function() {
        console.log("user_id" + wx.getStorageSync("user_id"));
        var e = this, a = wx.getStorageSync("wxapp").videoad;
        a && e.setData({
        }), "" != wx.getStorageSync("user_id") ? e.setData({
            is_login: 1
        }) : e.setData({
            is_login: 0
        });
    },
    uploadImage: function(e, a) {
        var t = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ a ],
            success: function(a) {
                var i = a.tempFilePaths[0];
                wx.showLoading({
                    title: "图片上传中..."
                }), wx.uploadFile({
                    url: "https://imgapi.faxingwu.com/api/upload",
                    filePath: i,
                    name: "image",
                    formData: {
                        image: i,
                        type: 1,
                        proname: "manhua"
                    },
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function(a) {
                        
                        wx.hideLoading();
                        var i = a.data, o = JSON.parse(i);
                        if (101 == o.code) {
                            var n = o.data[0];
                            console.log(n), wx.showLoading({
                                title: "图片检测中..."
                            }), wx.request({
                                url: "https://dou.j391.com/dongman/img_sec_check.php",
                                data: {
                                    p: "http://image.faxingwu.com" + n,
                                    appid: getApp().globalData.appid
                                },
                                method: "POST",
                                header: {
                                    "Content-Type": "application/x-www-form-urlencoded"
                                },
                                success: function(a) {
                                    let data={};
                                    data.errcode = 0;
                                    a.data =data;

                                    console.log("e.errcode=" + a.data.errcode), wx.hideLoading(), 0 == a.data.errcode ? (wx.setStorageSync("userImage", n), 
                                    t.makeInfo(n, e)) : wx.showToast({
                                        title: "内容含有违法违规内容",
                                        icon: "none",
                                        duration: 1e3
                                    });
                                }
                            });
                        } else wx.showToast({
                            title: "图片上传失败",
                            icon: "none",
                            duration: 1e3
                        });
                    }
                });
            }
        });
    },
    makeInfo: function(e, t) {
        if ("" == (i = wx.getStorageSync("uid"))) var i = 0;
        wx.showLoading({
            title: "图片绘制中..."
        }), wx.request({
            url: a.API_HOST + "/huihua/result/new-create-image",
            data: {
                uid: i,
                image: e,
                image_id: t
            },
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                if (console.log(e.data), 101 == e.data.code) {
                    var a = e.data.result_img;
                    wx.hideLoading(), wx.navigateTo({
                        url: "/pages/making/making?result_image=" + a
                    });
                } else wx.hideLoading(), wx.showToast({
                    title: "图片识别失败,请重新上传",
                    icon: "none",
                    duration: 1e3
                });
            }
        });
    },
    openCamera: function() {
        wx.setStorage({
            key: "image_id",
            data: 8
        }), this.uploadImage(8, "camera");
    },
    openAlbum: function() {
        wx.setStorage({
            key: "image_id",
            data: 8
        }), this.uploadImage(8, "album");
    },
    upload: function() {
        wx.setStorage({
            key: "image_id",
            data: 8
        }), this.uploadImage(8);
    },
    getUserInfo: function(e) {
        wx.navigateTo({
            url: "../login/login"
        });
    },
    getIndexData: function() {
        var a = this;
        e._get("index/page", {}, function(e) {
            a.setData(e.data);
        });
    },
    imagesHeight: function(e) {
        var a = e.target.dataset.id, t = e.target.dataset.itemKey, i = 750 / (e.detail.width / e.detail.height), o = this.data.imgHeights;
        void 0 === o[t] && (o[t] = {}), o[t][a] = i;
        var n = this.data.imgCurrent;
        void 0 === n[t] && (n[t] = Object.keys(this.data.items[t].data)[0]), this.setData({
            imgHeights: o,
            imgCurrent: n
        });
    },
    bindChange: function(e) {
        var a = e.target.dataset.itemKey, t = this.data.imgCurrent;
        t[a] = e.detail.currentItemId, this.setData({
            imgCurrent: t
        });
    },
    goTop: function(e) {
        this.setData({
            scrollTop: 0
        });
    },
    scroll: function(e) {
        this.setData({
            indexSearch: e.detail.scrollTop
        }), e.detail.scrollTop > 300 ? this.setData({
            floorstatus: !0
        }) : this.setData({
            floorstatus: !1
        });
    },
    onShareAppMessage: function() {
        return {
            title: "动漫人像生成器",
            path: "/pages/index/index",
            imageUrl: wx.getStorageSync("wxapp").navbar.imageUrl
        };
    }
});